﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_QLDCX : Form
    {
        public YP_QLDCX()
        {
            InitializeComponent();
        }


        public string sqdh;

        private void YP_QLDCX_Load(object sender, EventArgs e)//加载
        {
            dtp1_ypqldcx.Value = DateTime.Now.AddDays(-(DateTime.Now.Day - 1));//登录加载刷新的功能
            buttonItem1_Click_1(sender, e);

#region 申请科室显示
            getSqlConnection myconnection = new 药品信息管理.getSqlConnection();
            SqlConnection conn = myconnection.GetCon();
            SqlCommand mycomm = new SqlCommand("select mc from zd_ks", conn);
            SqlDataReader myread = mycomm.ExecuteReader();
            while (myread.Read())
            {
                this.cbB_sqyf_ypqldcx.Items.Add((string)myread[0]);
            }
            conn.Close();
#endregion
        }

        private void dataGridView1_ypqldcx_CellClick(object sender, DataGridViewCellEventArgs e)//显示对应申请单号的申请明细
        {
            textBoxItem1_ypqldcx.Text = Convert.ToString(dataGridView1_ypqldcx[0, dataGridView1_ypqldcx.CurrentCell.RowIndex].Value).Trim();
            textBoxItem2_ypqldcx.Text = Convert.ToString(dataGridView1_ypqldcx[1, dataGridView1_ypqldcx.CurrentCell.RowIndex].Value).Trim();
            textBoxItem3_ypqldcx.Text = Convert.ToString(dataGridView1_ypqldcx[2, dataGridView1_ypqldcx.CurrentCell.RowIndex].Value).Trim();
            textBoxItem4_ypqldcx.Text = Convert.ToString(dataGridView1_ypqldcx[3, dataGridView1_ypqldcx.CurrentCell.RowIndex].Value).Trim();
            textBoxItem6_ypqldcx.Text = Convert.ToString(dataGridView1_ypqldcx[4, dataGridView1_ypqldcx.CurrentCell.RowIndex].Value).Trim();
            db db2 = new db();
            sqdh = dataGridView1_ypqldcx.Rows[e.RowIndex].Cells[0].Value.ToString();
            string str = (@"SELECT A.SQDH,A.YPBM 药品编码,B.MC 名称,B.GG 规格,B.JLDW 单位,A.SQPS 申请数量
                            FROM YK_SQD1 A,ZD_YP B
                            WHERE A.YPBM=B.BM
                            AND A.SQDH=" + sqdh);
            dataGridViewX1.DataSource = db2.sqldt(str);
            dataGridViewX1.Columns["SQDH"].Visible = false;
        }

        private void rb1_ypqldcx_CheckedChanged(object sender, EventArgs e)//全部
        {
            db db2 = new db();
            string time1 = dtp1_ypqldcx.Value.ToString("yyyy.MM.dd"), time2 = dtp2_ypqldcx.Value.ToString("yyyy.MM.dd");
            string string_cbB_sqyf_ypqldcx = "";
            if (cbB_sqyf_ypqldcx.SelectedText != "") { string_cbB_sqyf_ypqldcx = "and B.MC='" + cbB_sqyf_ypqldcx.SelectedText + "' "; }

            string str = (@"SELECT A.SQDH 申请单号,convert(datetime,A.SQRQ-2) SQRQ,B.MC,A.SQRY s,C.CKDH
                            FROM YK_SQD0 A
                            LEFT JOIN YK_CKD0 C ON A.SQDH=C.SQDH, ZD_KS B
                            WHERE A.KSBM=B.BM
                            AND A.SQRQ>" + db2.datetoint(time1) + " AND A.SQRQ<= " + db2.datetoint(time2) + string_cbB_sqyf_ypqldcx); 
            str = "select * from (" + str + ") a order by 申请单号";
            dataGridView1_ypqldcx.DataSource = db2.sqldt(str);
            dataGridView1_ypqldcx.Columns["SQRQ"].Visible = false;
            dataGridView1_ypqldcx.Columns["MC"].Visible = false;
            dataGridView1_ypqldcx.Columns["CKDH"].Visible = false;
            dataGridView1_ypqldcx.Columns["s"].Visible = false;
        }

        private void rb2_ypqldcx_CheckedChanged(object sender, EventArgs e)//未发药
        {
            db db3 = new db();
            string time1 = dtp1_ypqldcx.Value.ToString("yyyy.MM.dd"), time2 = dtp2_ypqldcx.Value.ToString("yyyy.MM.dd");
            string string_cbB_sqyf_ypqldcx = "";
            if (cbB_sqyf_ypqldcx.SelectedText != "") { string_cbB_sqyf_ypqldcx = "and B.MC='" + cbB_sqyf_ypqldcx.SelectedText + "' "; }

            string str = (@"SELECT A.SQDH 申请单号,convert(datetime,A.SQRQ-2) SQRQ,B.MC,A.SQRY s,C.CKDH
                            FROM YK_SQD0 A
                            LEFT JOIN YK_CKD0 C ON A.SQDH=C.SQDH, ZD_KS B
                            WHERE A.KSBM=B.BM AND A.FLAG=1
                            AND A.SQRQ>" + db3.datetoint(time1) + " AND A.SQRQ<= " + db3.datetoint(time2) + string_cbB_sqyf_ypqldcx); 
            str = "select * from (" + str + ") a order by 申请单号";
            dataGridView1_ypqldcx.DataSource = db3.sqldt(str);
            dataGridView1_ypqldcx.Columns["SQRQ"].Visible = false;
            dataGridView1_ypqldcx.Columns["MC"].Visible = false;
            dataGridView1_ypqldcx.Columns["CKDH"].Visible = false;
            dataGridView1_ypqldcx.Columns["s"].Visible = false;
        }

        private void rb3_ypqldcx_CheckedChanged(object sender, EventArgs e)//已发药
        {
            db db4 = new db();
            string time1 = dtp1_ypqldcx.Value.ToString("yyyy.MM.dd"), time2 = dtp2_ypqldcx.Value.ToString("yyyy.MM.dd");
            string string_cbB_sqyf_ypqldcx = "";
            if (cbB_sqyf_ypqldcx.SelectedText != "") { string_cbB_sqyf_ypqldcx = "and B.MC='" + cbB_sqyf_ypqldcx.SelectedText + "' "; }

            string str = (@"SELECT A.SQDH 申请单号,convert(datetime,A.SQRQ-2) SQRQ,B.MC,A.SQRY s,C.CKDH
                            FROM YK_SQD0 A
                            LEFT JOIN YK_CKD0 C ON A.SQDH=C.SQDH, ZD_KS B
                            WHERE A.KSBM=B.BM AND A.FLAG=3
                            AND A.SQRQ>" + db4.datetoint(time1) + " AND A.SQRQ<= " + db4.datetoint(time2) + string_cbB_sqyf_ypqldcx); 
            str = "select * from (" + str + ") a order by 申请单号";
            dataGridView1_ypqldcx.DataSource = db4.sqldt(str);
            dataGridView1_ypqldcx.Columns["SQRQ"].Visible = false;
            dataGridView1_ypqldcx.Columns["MC"].Visible = false;
            dataGridView1_ypqldcx.Columns["CKDH"].Visible = false;
            dataGridView1_ypqldcx.Columns["s"].Visible = false;
        }


        private void buttonItem1_Click_1(object sender, EventArgs e)//刷新
        {
            db db1 = new db();
            string time1 = dtp1_ypqldcx.Value.ToString("yyyy.MM.dd"), time2 = dtp2_ypqldcx.Value.ToString("yyyy.MM.dd");
            string string_cbB_sqyf_ypqldcx = "";
            if (cbB_sqyf_ypqldcx.SelectedText != "") { string_cbB_sqyf_ypqldcx = "and B.MC='" + cbB_sqyf_ypqldcx.SelectedText + "' "; }

            string str = (@"SELECT A.SQDH 申请单号,convert(datetime,A.SQRQ-2) SQRQ,B.MC,A.SQRY s,C.CKDH
                            FROM YK_SQD0 A
                            LEFT JOIN YK_CKD0 C ON A.SQDH=C.SQDH, ZD_KS B
                            WHERE A.KSBM=B.BM
                            AND A.SQRQ>" + db1.datetoint(time1) + " AND A.SQRQ<= " + db1.datetoint(time2) + string_cbB_sqyf_ypqldcx); 
            str = "select * from (" + str + ") a order by 申请单号";
            dataGridView1_ypqldcx.DataSource = db1.sqldt(str);
            dataGridView1_ypqldcx.Columns["SQRQ"].Visible = false;
            dataGridView1_ypqldcx.Columns["MC"].Visible = false;
            dataGridView1_ypqldcx.Columns["CKDH"].Visible = false;
            dataGridView1_ypqldcx.Columns["s"].Visible = false;
        }

        private void buttonItem2_Click(object sender, EventArgs e)//打印
        {
            dgVprint1.Print(dataGridViewX1, true);
        }

        private void buttonItem3_Click(object sender, EventArgs e)//导出Excel
        {
            db db1 = new db();
            db1.DataToExcel(dataGridView1_ypqldcx);
        }

      
    }
}